﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Controls;
using GameSPK.Model;
using GameSPK.Helpers;
using System.Windows.Media;
using GameSPK.DAO;
using System.Windows.Media.Imaging;
using System.Windows;


namespace GameSPK.Start
{
    class frmChonCauHoi: frmGenerel
    {
        private static frmChonCauHoi instance;
        private static object syncLock = new object();
        int maphanthi;
        public static frmChonCauHoi getInstance(int maPT)
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        
                        instance = new frmChonCauHoi(maPT);
                    }
                }
            }

            return instance;
        }
        private frmChonCauHoi(int maPT)
            : base()
        {
            maphanthi = maPT;
            this.Closed += new EventHandler(frmChonCauHoi_Closed);
            this.Loaded += new System.Windows.RoutedEventHandler(frmChonCauHoi_Loaded);
            AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)frmChonCauHoi_KeyDown);
            
        }
        void frmChonCauHoi_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
        }
        void frmChonCauHoi_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            filename = "section" + maphanthi + "mainlayout.xml";
            socauhoi = PhanThiDAO.getSoCauHoi(maphanthi, MainWindow.getCuocThiChon());
            Generate();
        }
        string filename;
        void Generate()
        {
            Button btnHome;
            Button btnTheLe;
            Control cbtnHome, cbtnTheLe;
            btnHome = new Button();
            cbtnHome = (Control)btnHome;
            LabelLayout lableXML;
            lableXML = Helper.getButtonFromXML(filename, "HomeButton");
            AssignControl.assignControlLayout(ref cbtnHome, lableXML, "btnHome");
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getHomeButtonUrl(), UriKind.RelativeOrAbsolute));
            btnHome.Background = myBrush;
            btnHome.Click += new RoutedEventHandler(btnHome_Click);
            gridMain.Children.Add(btnHome);

            btnTheLe = new Button();
            cbtnTheLe = (Control)btnTheLe;

            lableXML = Helper.getButtonFromXML(filename, "TheLeButton");
            AssignControl.assignControlLayout(ref cbtnTheLe, lableXML, "btnTheLe");
            ImageBrush myBrush2 = new ImageBrush();
            myBrush2.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getHelpButtonUrl(), UriKind.RelativeOrAbsolute));
            btnTheLe.Background = myBrush2;
            gridMain.Children.Add(btnTheLe);
            //hien thi cac luc chon cau hoi
            for (int i = 0; i < socauhoi; i++)
            {
                Label lblGoiCauHoi;
                Control clblGoiCauHoi;
                lblGoiCauHoi = new Label();
                clblGoiCauHoi = (Control)lblGoiCauHoi;
                lableXML = Helper.getGoiCauHoiFromXML(filename,  i);
                AssignControl.assignControlLayout(ref clblGoiCauHoi, lableXML, "lblgoicauhoi" + i);
                lblGoiCauHoi.Foreground = lableXML.Foreground;
                lblGoiCauHoi.Content = ""+(i+1);
                lblGoiCauHoi.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                lblGoiCauHoi.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                lblGoiCauHoi.FontSize = lableXML.FontSize;
                lblGoiCauHoi.FontWeight = System.Windows.FontWeights.Bold;
                lblGoiCauHoi.Style = (Style)this.FindResource("PulseLabel");
                DrawingBrush myDrawingBrush = new DrawingBrush();
                GeometryDrawing myGeometryDrawing = new GeometryDrawing();
                myGeometryDrawing.Brush = lableXML.Background;

                myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
                GeometryGroup ellipses = new GeometryGroup();
                ellipses.Children.Add(new EllipseGeometry(new Point(25, 50), 50, 50));
                myGeometryDrawing.Geometry = ellipses;
                myDrawingBrush.Drawing = myGeometryDrawing;
                lblGoiCauHoi.Background = myDrawingBrush;
                lblGoiCauHoi.MouseDoubleClick += new MouseButtonEventHandler(lblGoiCauHoi_MouseDoubleClick);

                gridMain.Children.Add(lblGoiCauHoi);
            }


        }

        void btnHome_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void lblGoiCauHoi_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            int goi;
            goi = int.Parse(((Label)sender).Name.Replace("lblgoicauhoi", ""));
            if (maphanthi == 1)
            {
                frmSection1Start phan1 = frmSection1Start.getInstance(goi+1);
                phan1.ShowDialog();
                phan1.Focus();
            }
            else if (maphanthi == 2)
            {
                frmSection2Start phan2 = frmSection2Start.getInstance(goi + 1);
                phan2.ShowDialog();
                phan2.Focus();
            
            }
            else if (maphanthi == 5)
            {
                frmSection5Start phan5 = frmSection5Start.getInstance(goi + 1);
                phan5.ShowDialog();
                phan5.Focus();

            }
            else if (maphanthi == 4)
            {
                frmSection4Start phan4 = frmSection4Start.getInstance(goi + 1);
                phan4.ShowDialog();
                phan4.Focus();

            }
            Label lblGoi = (Label)sender;
            lblGoi.Style = null;
            DrawingBrush myDrawingBrush = new DrawingBrush();
            GeometryDrawing myGeometryDrawing = new GeometryDrawing();
            myGeometryDrawing.Brush = Brushes.DarkBlue;

            myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
            GeometryGroup ellipses = new GeometryGroup();
            ellipses.Children.Add(new EllipseGeometry(new Point(25, 50), 50, 50));
            myGeometryDrawing.Geometry = ellipses;
            myDrawingBrush.Drawing = myGeometryDrawing;
            lblGoi.Background = myDrawingBrush;
            
            

        }
        int socauhoi;
        void frmChonCauHoi_Closed(object sender, EventArgs e)
        {
            instance = null;
        }
    }
}
